<?php
/***
 * 消息模块后台控制器
 *
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Response, Request as Input;
use App\Services\ImgServices\ImgTool;
use App\Services\Tool\Jpush\JpushTool;
use App\Services\JPush\JPushService;

class JpushController extends BaseController
{

    protected $imgTool;

    public function __construct(ImgTool $imgTool)
    {
        $this->imgTool = $imgTool;
    }

    /**
     * 推送消息页面
     */
    public function messageIndex()
    {
        return view('backstage.jpush.themeMessage');
    }

    /**
     * 推送
     */
    public function pushSystemMessage(Request $request)
    {
        $input  = Input::all();
        // dd($input);
        if(!isset($input['template_no']) ||
            !isset($input['type']) ||
            !isset($input['platform']) ||
            !isset($input['phone_message']) ||
            !isset($input['title']) ||
            !isset($input['app_message']) ||
            !isset($input['jump_value']))
            return redirect()->back()->with('error_message', '参数不足');

        $image = $request->file('image');
        $app_message = $request->get('app-message');
        $class = 'system_notification';
        $user_id = 0;
        if (!empty($image)) {
            $img_path_org = $this->imgTool->imgSave($image, 2);
            $image_thumbnail_url = $this->imgTool->getImgThumbByImgPath($img_path_org, 600, 600);
        } else {
            $img_path_org = '';
            $image_thumbnail_url = '';
        }

        if (($input['template_no'] == 1 || $input['template_no'] == 3 || $input['template_no'] == 4) && empty($image)) {
            return redirect()->back()->with('error_message', '图片不能为空');
        }
        if($input['type'] == 0 && $input['jump_value'] == ''){
            return redirect()->back()->with('error_message', '链接不能为空');
        }

        $input['platform'] = JPushService::get_platform($input['platform']);

        if(!isset($input['send_time']) || !strtotime($input['send_time'])){
            $result = JpushTool::push($input['phone_message'], $input['platform'], $user_id,
            [
                'title' => $input['title'],
                'user_id' => $user_id,
                'class' => $class,
                'type' => $input['type'],
                'jump_value' => $input['jump_value'],
                'template_no' => $input['template_no'],
                'message' => $input['app_message'],
                'images' => [['original_image' => $img_path_org,
                    'thumbnail' => $image_thumbnail_url]]
            ]);
        }else{
            $input['send_time'] = $input['send_time'] . ' ' . (isset($input['hour'])? $input['hour'] . ':00:00' : '');
            if(!strtotime($input['send_time'])) return redirect()->back()->with('error_message', '推送时间格式错误');
            //定时
            $data = [
                     'notice_title' => $input['phone_message'],
                     'platform'     => $input['platform'],
                     'user_id'      => $user_id,
                     'extras'       => [
                        'title' => $input['title'],
                        'user_id' => $user_id,
                        'class' => $class,
                        'type' => $input['type'],
                        'jump_value' => $input['jump_value'],
                        'template_no' => $input['template_no'],
                        'message' => $input['app_message'],
                        'images' => [['original_image' => $img_path_org,
                            'thumbnail' => $image_thumbnail_url]]
                     ]
                    ];
            $result = JPushService::add_timing_job([
                'send_time'   => strtotime($input['send_time']),
                'app_id'      => 1,
                'resource_id' => '',
                'data_json'   => json_encode($data)
            ]);
            $result = $result['state'] == 'M00000';
        }


        if($result == false){
            return redirect()->back()->with('error_message', '推送失败,请重试');
        }else{
            return redirect()->back()->with('success_message', '推送成功');
        }
    }
}
